<?php

require_once(WSB_MODULES_PATH .'sherpa/models/BaseGateway.php');
require_once(WSB_MODULES_PATH .'sherpa/models/Products.php');
require_once(WSB_MODULES_PATH .'sherpa/models/Product.php');

class Sherpa_Model_ProductGateway extends Sherpa_Model_BaseGateway
{

	/**
	 * @var integer
	 */
	private $_itemCountPerPage = 3;

	/**
	 * @param integer $itemCountPerPage
	 */
	public function setItemCountPerPage($itemCountPerPage)
	{
		$this->_itemCountPerPage = intval($itemCountPerPage);
	}

	/**
	 * @return integer
	 */
	public function getItemCountPerPage()
	{
		return $this->_itemCountPerPage;
	}

	/**
	 * @param integer $categoryId
	 * @param integer $languageId
	 * @param integer $page
	 * @return Sherpa_Model_Products|Zend_Paginator
	 */
	public function fetchParents($categoryId, $languageId, $page)
	{
    	$select = $this->getDbSelect();
		$select->distinct();
		$select->from(array('p' => 'sherpa_products'));
		$select->join(array('ptc' => 'sherpa_productToCategory'), 'p.id = ptc.parentProductId', array());
		$select->where('p.parentId IS NULL');
		$select->where('p.languageId = ?', $languageId);
		$select->where('ptc.categoryId = ?', $categoryId);
		$select->where('p.active = 1');
		//$select->where('p.available = 1');
		$select->where('p.archived IS NULL');
		$select->order('p.sort ASC');

		//$this->_setActiveWhereClause($select, 'p.activeFrom', 'p.activeTo');
		//$this->_setAvailableWhereClause($select, 'p.availableFrom', 'p.availableTo');
		if($this->usePaginator()) {
            $page = empty($page) ? 1 : intval($page);
            $paginator = new Zend_Paginator(
                new Zend_Paginator_Adapter_DbSelect($select)
            );
            $paginator->setItemCountPerPage($this->getItemCountPerPage())
                      ->setCurrentPageNumber($page);
            return $paginator;
		} else {
			$stmt = $select->query();
			return new Sherpa_Model_Products($stmt->fetchAll());
		}
	}

	/**
	 * @param integer $id
	 * @param integer $languageId
	 * @return Sherpa_Model_Product
	 */
	public function fetchParent($id, $languageId)
	{
		$select = $this->getDbSelect();
		$select->distinct();
		$select->from(array('p' => 'sherpa_products'));
		$select->joinLeft(array('st' => 'sherpa_sizeTables'), 'st.id = p.sizeTableId', array('sizeTableHtml' => 'tableHtml'));
		$select->join(array('ptc' => 'sherpa_productToCategory'), 'p.id = ptc.parentProductId', array());
		$select->where('p.parentId IS NULL');
		$select->where('p.id = ?', $id);
		$select->where('p.languageId = ?', $languageId);
		$select->where('p.active = 1');
		//$select->where('p.available = 1');
		$select->where('p.archived IS NULL');
		//$this->_setActiveWhereClause($select, 'p.activeFrom', 'p.activeTo');
		//$this->_setAvailableWhereClause($select, 'p.availableFrom', 'p.availableTo');
		$stmt = $select->query();
		return new Sherpa_Model_Product((array)$stmt->fetch());
	}

	/**
	 * @param integer $categoryId
	 * @param integer $languageId
	 * @return array|Sherpa_Model_Products
	 */
	public function fetchChildren($parentId, $languageId)
	{
		$select = $this->getDbSelect();
		$select->distinct();
		$select->from(array('p' => 'sherpa_products'));
		$select->join(array('s' => 'sherpa_sizes'), 's.id = p.sizeId', array('size' => 's.size'));
		$select->where('p.parentId = ?', $parentId);
		$select->where('p.languageId = ?', $languageId);
		$select->where('p.active = 1');
		//$select->where('p.available = 1');
		$select->where('p.archived IS NULL');
		//$this->_setActiveWhereClause($select, 'p.activeFrom', 'p.activeTo');
		//$this->_setAvailableWhereClause($select, 'p.availableFrom', 'p.availableTo');
		$stmt = $select->query();
		if($this->useModels()) {
			return new Sherpa_Model_Products($stmt->fetchAll());
		} else {
			return $stmt->fetchAll();
		}
	}

}